﻿namespace EWrap
{
    /// <summary>
    /// 结果包装类
    /// </summary>
    public class EasyResult
    {
        /// <summary>
        /// 制作结果
        /// </summary>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息，不指定则根据code设置为成功或失败</param>
        /// <returns>结果</returns>
        public static EasyResult Make(string msg = "成功", int code=1)
        {
            if (string.IsNullOrEmpty(msg))
            {
                msg = code > 0 ? "成功" : "失败";
            }
            else 
            {
                code = msg == "成功" ? 1 : -1;
            }
            return new EasyResult()
            {
                Msg=msg,
                Code=code
            };
        }
        /// <summary>
        /// 制作成功结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="value">值</param>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息，默认值成功</param>
        /// <returns>成功结果</returns>
        public static EasyResult<T> Success<T>(T value = default(T), string msg = "成功",int code=1)
        {
            return new EasyResult<T>()
            {
                Code=code,
                Msg = msg,
                Value = value
            };
        }

        /// <summary>
        /// 制作失败结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息，默认值失败</param>
        /// <returns>失败结果</returns>
        public static EasyResult<T> Fail<T>(string msg = "失败", int code = -1)
        {
            return new EasyResult<T>
            {
                Code=code,
                Msg = msg,
                Value=default(T)
            };
        }

        /// <summary>
        /// 自定义状态码，一般大于0表示成功，小于等于0表示失败，特殊约定除外
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; } = "成功";
    }

    /// <summary>
    /// 结果包装泛型类
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class EasyResult<T> : EasyResult
    {
        /// <summary>
        /// 值
        /// </summary>
        public T Value { get; set; } = default(T);
    }
}
